/**
 * Created by lixiaodi on 2019/06/28 0027.
 */
mini.parse();
var grid = mini.get("datagrid1");
var tree = mini.get("tree1");
var depIds;
//加载树结构
$(function(){
	//加载部门树
	treeHttp();
});
//父页面给子页面传值
function SetData(data){
	depIds = data.addData;
}
//子页面给父页面传值
function GetData() {
	var rows = grid.getSelecteds();
    return rows;
}
//点击树节点的时候显示
tree.on("nodeselect", function (e) {
    //被选中得节点
    var selecNode = e.node;
    //如果点击了节点
    if (selecNode) {
        ajaxBack(selecNode);
    } else {
        grid.setData([]);
        grid.setTotalCount(0);
    }
});
//确定保存
function onSave() {
    var rows = grid.getSelecteds();
    if(rows){
    	CloseWindow("save");
    }
}
//关闭子页面
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
	onSave();
}
function onCancel(e) {
    CloseWindow("cancel");
}
//点击树得到数据表
function ajaxBack(selecNode){
    $.ajax({
        url : ajaxUrl + "/DepartmentController/getlistByPid.do",
        type : "POST",
        data : {
            id : selecNode.pk,
            types : selecNode.id.substring(0,2)
        },
        success : function(data){
            if(data.data){
                grid.setData(data.data);
                var depData = data.data;
                //选中之前选过的
                if(depIds){
            		var depIdsArr = depIds.split(',');
            		depData.forEach(function (item,index){
            			var listItem = item;
            			depIdsArr.forEach(function (item,index){
            				if(listItem.departmentId == item){
            					//勾选
            					grid.select(listItem,true);
            				}
            			});
            		});
            	}else{
            		//console.log('暂无被勾选的人员');
            	}
            }else{
                showTips(data.msg);
                grid.setData(data.data);
            }
        },
        error : function(data){
            mini.alert(data.msg);
        }
    })
}
//tree请求
function treeHttp(){
	$.ajax({
        url : ajaxUrl + "/TreeController/getAllTree.do",
        type:"post",
        success: function (data) {
        	var selecNode = data.data[0].children[0];
            tree.setData(data.data);
            ajaxBack(selecNode);
        },
        error:function(data){
            mini.alert("加载失败");
        }
    })
}